Structure ProcInfo
  name.s
  pid.l
EndStructure

Global NewList Procs.ProcInfo()
Global hProcess, retPID

Procedure GetProcessList()
  handle = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
  Define pe.PROCESSENTRY32
  pe\dwSize = SizeOf(PROCESSENTRY32)
  pret = Process32First_(handle, @pe)
  cnt=0
  While pret = 1
    With Procs()
      AddElement(Procs())
      \name=PeekS(@pe\szExeFile)
      \pid=pe\th32ProcessID
    EndWith
    
    ;Debug Str(procs()\pid) + " - " + Procs()\name
    
    pret = Process32Next_(handle, @pe)
    cnt+1
  Wend
EndProcedure

Procedure ChoosePID()
  ; see if we have a valid index chosen
  index = GetGadgetState(#PROC_LIST_PROCS)
  If index <> -1
    tmp$=GetGadgetText(#PROC_LIST_PROCS)
    tmp$=Mid(tmp$, 1, FindString(tmp$, ":", 1)-1)
  EndIf
  
  If Val(tmp$) <> 0
    retPID = Val(tmp$)
    SetWindowTitle(#MAIN_WINDOW, "Mem Scanner - [ " + GetGadgetText(#PROC_LIST_PROCS) + " ]")
    DisableWindow(#MAIN_WINDOW, 0)
    CloseWindow(#PROC_WINDOW)
    
    ;we have a proc id, lets try to open it
    hProcess = OpenProcess_(#PROCESS_ALL_ACCESS, #False, retPID)
    If Not hProcess
      ;give error could not open pid
      MessageRequester("Could not open PID!", "Could not open PID " + Str(retPID))
    EndIf
  EndIf
  
  ProcedureReturn retPID
EndProcedure

;fill/init gadgets on main window
Procedure InitMainWindowGadgets()
  ;filling main screen scan type combo
  AddGadgetItem(#MAIN_COMBO_SCAN_TYPE, -1, "Exact Value")
  AddGadgetItem(#MAIN_COMBO_SCAN_TYPE, -1, "Greater Than...")
  AddGadgetItem(#MAIN_COMBO_SCAN_TYPE, -1, "Less Than...")
  AddGadgetItem(#MAIN_COMBO_SCAN_TYPE, -1, "Value Between...")
  AddGadgetItem(#MAIN_COMBO_SCAN_TYPE, -1, "Unknown Initial Value")
  SetGadgetState(#MAIN_COMBO_SCAN_TYPE, 0)
  
  ;filling main screen value type combo
  AddGadgetItem(#MAIN_COMBO_VALUE_TYPE, -1, "Binary")
  AddGadgetItem(#MAIN_COMBO_VALUE_TYPE, -1, "Byte")
  AddGadgetItem(#MAIN_COMBO_VALUE_TYPE, -1, "2 Bytes")
  AddGadgetItem(#MAIN_COMBO_VALUE_TYPE, -1, "4 Bytes")
  AddGadgetItem(#MAIN_COMBO_VALUE_TYPE, -1, "8 Bytes")
  AddGadgetItem(#MAIN_COMBO_VALUE_TYPE, -1, "Double")
  AddGadgetItem(#MAIN_COMBO_VALUE_TYPE, -1, "Text")
  AddGadgetItem(#MAIN_COMBO_VALUE_TYPE, -1, "Array of Bytes")
  AddGadgetItem(#MAIN_COMBO_VALUE_TYPE, -1, "All - Byte to Double")
  AddGadgetItem(#MAIN_COMBO_VALUE_TYPE, -1, "Custom")
  SetGadgetState(#MAIN_COMBO_VALUE_TYPE, 3)
  
  ;-Set Next Scan button disabled
  DisableGadget(#MAIN_NEXTSCN, 1)
  DisableGadget(#MAIN_UNDOSCN, 1)
  
  ;-Hide Buttons
  HideGadget(#MAIN_CHK_UNICODE, 1)
  HideGadget(#MAIN_CHK_CASE, 1)
  
  ;-Set Next 
EndProcedure

Procedure InitEditWindowGadgets()
    
  ;filling code edit value type combo
  AddGadgetItem(#EDIT_COMBO_VALUE_TYPE, -1, "Binary")
  AddGadgetItem(#EDIT_COMBO_VALUE_TYPE, -1, "Byte")
  AddGadgetItem(#EDIT_COMBO_VALUE_TYPE, -1, "2 Bytes")
  AddGadgetItem(#EDIT_COMBO_VALUE_TYPE, -1, "4 Bytes")
  AddGadgetItem(#EDIT_COMBO_VALUE_TYPE, -1, "8 Bytes")
  AddGadgetItem(#EDIT_COMBO_VALUE_TYPE, -1, "Double")
  AddGadgetItem(#EDIT_COMBO_VALUE_TYPE, -1, "Text")
  AddGadgetItem(#EDIT_COMBO_VALUE_TYPE, -1, "Array of Bytes")
  AddGadgetItem(#EDIT_COMBO_VALUE_TYPE, -1, "All - Byte to Double")
  AddGadgetItem(#EDIT_COMBO_VALUE_TYPE, -1, "Custom")
  SetGadgetState(#EDIT_COMBO_VALUE_TYPE, 3)
EndProcedure

Procedure CallMainWindow()
  Open_MAIN_WINDOW()
  InitMainWindowGadgets()
EndProcedure
        
Procedure CallCodeEditWindow()
  ;window is modal so disable main window and make this sticky
  DisableWindow(#MAIN_WINDOW, 1)
  Open_CODE_EDIT_WINDOW()
  InitEditWindowGadgets()
  StickyWindow(#CODE_EDIT_WINDOW, 1)
EndProcedure

Procedure CallProcChooser()
  ;window is modal so disable main window and make this sticky
  DisableWindow(#MAIN_WINDOW, 1)
  Open_PROC_WINDOW()
  StickyWindow(#PROC_WINDOW, 1)
  
  ;get list of processes
  GetProcessList()

  ;fill listview with our process list
  ForEach Procs()
    AddGadgetItem(#PROC_LIST_PROCS, 0, Str(Procs()\pid) + ":" + Procs()\name)
  Next
EndProcedure

Procedure GetValueType(text.s)
  Select text
    Case "4 Bytes"
      valuetype=#SRCH_VALUE
    Case "Text"
      valuetype=#SRCH_TEXT
    Default 
      valueType=#SRCH_NOTSET
  EndSelect
  
  ProcedureReturn valuetype
EndProcedure


; IDE Options = PureBasic 4.51 (Windows - x86)
; CursorPosition = 23
; FirstLine = 5
; Folding = +-
; EnableXP